﻿using System.Xml.Linq;
using Engine;
using Engine.Graphics;
using Engine.Media;
using Game;

namespace MFTrain
{
    public class MFEngineWidget : CanvasWidget
    {
        public GridPanelWidget m_inventoryGrid;

        public InventorySlotWidget m_fuelSlot;

        public FireWidget m_fire;

        public ComponentMFEngine m_componentFurnace;

        public ComponentMFChestCache componentChestCache;

        public MFWaterWidget waterWidget;

        public GridPanelWidget chestCacheGrid;

        public BitmapButtonWidget breakButton;

        public LabelWidget title;

        public MFEngineWidget(IInventory inventory, ComponentMFEngine componentFurnace, ComponentMFChestCache componentChestCache)
        {
            m_componentFurnace = componentFurnace;
            this.componentChestCache = componentChestCache;
            XElement node = ContentManager.Get<XElement>("MF/Train/EngineWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_fire = Children.Find<FireWidget>("Fire");
            m_fuelSlot = Children.Find<InventorySlotWidget>("FuelSlot");
            waterWidget = Children.Find<MFWaterWidget>("Water");
            chestCacheGrid = Children.Find<GridPanelWidget>("CacheChestGrid");
            breakButton = Children.Find<BitmapButtonWidget>("BreakButton");
            title = Children.Find<LabelWidget>("Title");
            breakButton.IsAutoCheckingEnabled = true;
            Image image = new Image(1, 1);
            image.Pixels[0] = new Color(16, 16, 16, 128);
            Subtexture clickedSubtexture = new Subtexture(Texture2D.Load(image), Vector2.Zero, Vector2.One);
            Image image2 = new Image(1, 1);
            image2.Pixels[0] = Color.Transparent;
            Subtexture normalSubtexture = new Subtexture(Texture2D.Load(image2), Vector2.Zero, Vector2.One);
            breakButton.ClickedSubtexture = clickedSubtexture;
            breakButton.NormalSubtexture = normalSubtexture;
            breakButton.IsChecked = m_componentFurnace.engineBreak;
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            m_fuelSlot.AssignInventorySlot(componentFurnace, 0);
            for (int k = 0; k < 2; k++)
            {
                Vector2 margin = new Vector2(28f, 52 + k * 72);
                InventorySlotWidget inventorySlotWidget2 = new InventorySlotWidget();
                inventorySlotWidget2.Margin = margin;
                inventorySlotWidget2.BevelColor = Color.Transparent;
                inventorySlotWidget2.CenterColor = Color.Transparent;
                inventorySlotWidget2.AssignInventorySlot(componentFurnace, 1);
                inventorySlotWidget2.HideEditOverlay = true;
                inventorySlotWidget2.HideInteractiveOverlay = true;
                inventorySlotWidget2.HideFoodOverlay = true;
                inventorySlotWidget2.HideHighlightRectangle = true;
                inventorySlotWidget2.HideBlockIcon = true;
                Children.Add(inventorySlotWidget2);
            }

            num = 0;
            for (int l = 0; l < chestCacheGrid.RowsCount; l++)
            {
                for (int m = 0; m < chestCacheGrid.ColumnsCount; m++)
                {
                    InventorySlotWidget inventorySlotWidget3 = new InventorySlotWidget();
                    inventorySlotWidget3.AssignInventorySlot(componentChestCache, num++);
                    chestCacheGrid.Children.Add(inventorySlotWidget3);
                    chestCacheGrid.SetWidgetCell(inventorySlotWidget3, new Point2(m, l));
                }
            }
        }

        public override void Update()
        {
            m_componentFurnace.engineBreak = breakButton.IsChecked;
            waterWidget.Color = m_componentFurnace.GetLocalWaterColor();
            waterWidget.ParticlesPerSecond = ((!m_componentFurnace.engineBreak && m_componentFurnace.fireTimeRemaining > 0f) ? (6f * m_componentFurnace.fuelLevel) : 0f);
            waterWidget.Value = m_componentFurnace.waterVolumn;
            m_fire.ParticlesPerSecond = ((!m_componentFurnace.engineBreak && m_componentFurnace.fireTimeRemaining > 0f) ? (12f * m_componentFurnace.fuelLevel) : 0f);
            title.Text = "蒸汽机车" + (m_componentFurnace.engineBreak ? "(制动)" : "");
            if (!m_componentFurnace.IsAddedToProject)
            {
                base.ParentWidget.Children.Remove(this);
            }
        }
    }
}